package linkedlistdemo;

import com.sun.xml.internal.ws.api.server.Adapter;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-12
 * Time:15:56
 */

class Node {
    public int val;
    public Node next;//类型是Node  null

    public Node(int val) {
        this.val = val;
    }
}
public class SingleLinkedList {

    public Node head;//为什么定义到这里
    public int usedSize;//记录当前链表中节点的个数
    private int length;

    /**
     * 先来了解一下链表的结构
     * 使用穷举的方式，创建链表
     */
    public void createList() {
        Node node1 = new Node(12);
        Node node2 = new Node(23);
        Node node3 = new Node(34);
        Node node4 = new Node(45);
        Node node5 = new Node(56);
        //int a = 10;
        node1.next = node2;//？
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
        this.usedSize = 5;
    }


    /**
     * 打印链表里面的元素
     */
    public void myToString(){
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        return false;
    }

    //得到单链表的长度-》尝试不用我们定义的usedSize
    public int size(){
        return -1;
    }


    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(head!=null){
            node.next=head;
            head=node;
            this.head.next=null;
        }else{
            head=node;
            this.head.next=null;
        }
        this.usedSize++;
    }
    //尾插法
    public void addLast(int data){

    }

    /**
     * 查找Index-1位置的节点的地址
     * @param index
     * @return
     */
/*    private Node searchIndex(int index) {
        
    }*/

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > this.usedSize) {
            throw new RuntimeException("index不合法！");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.usedSize) {
            addLast(data);
            return;
        }
    }



    //删除第一次出现关键字为key的节点
 /*   public void remove(int key){
        if(this.head == null) {
            return;
        }
        if(this.head.next == null) {
            //只有1个节点
            this.head = null;//把头节点的引用置为空
            return;
        }
        Node prev = findPrevNode(key);

    }*/
    /**
     * 找到关键字Key的前一个节点
     * @param key
     * @return 不存在 返回null 存在返回节点的地址
     */
    //private Node findPrevNode(int key) {
    //}

    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != null) {
            if (cur.next.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
            if (this.head.val == key ){
                this.head = this.head.next;
                this.usedSize--;
            }
        }

    }


    public void clear(){

    }

}
